
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct
{
    char apn[32];
    char mccs[32];
}CL_APN_S;

typedef struct
{
    short mcc;
	short apn_count;
    CL_APN_S *apn_data;
}CL_APN_DATA_S;
	
#define CL_APN_LANG_CN // 中国
#define CL_APN_LANG_VN // 越南
#define CL_APN_LANG_MY // 马来西亚


static CL_APN_S _apn_lang_cn_460[] = {
  {"cmnet", "000202"},
  {"uninet", "01"},
};
static CL_APN_S _apn_lang_vn_452[] = {
  {"m-wap", "01"},
  {"m3-world", "02"},
  {"v-internet", "04"},
  {"internet", "0305060708"},
};
static CL_APN_S _apn_lang_my_502[] = {
  {"net", "011011"},
  {"celcom.net.my", "13141920"},
  {"diginet", "16"},
  {"internet.gprs.maxis", "171215"},
};

static CL_APN_DATA_S _apns[] = {
    {460, 2, _apn_lang_cn_460},
    {452, 4, _apn_lang_vn_452},
    {502, 4, _apn_lang_my_502},
};

void cl_GetApnData(char *strImsi, char *strNetApn)
{
    int i=0;
    int max = sizeof(_apns)/sizeof(CL_APN_DATA_S);
    char mcc_str[4] = {0};
    char mnc_str[4] = {0};
    int mcc = 460;
    
    // 转换
    strncpy(mcc_str, strImsi, 3);
    strncpy(mnc_str, strImsi+3, 2);
    mcc = atoi(mcc_str);

    for (i = 0; i < max; i++)
    {
        CL_APN_DATA_S *apn = &_apns[i];        
        int apn_count = sizeof(*apn->apn_data)/sizeof(CL_APN_S);
        int j=0;

        for(j=0;j<apn->apn_count;j++)
        {
            char *p = apn->apn_data[j].mccs;
            if(mcc!=apn->mcc)
                continue;

            while(*p>0) {
                if(memcmp(p, mnc_str, 2) ==0){
                    strcpy(strNetApn, apn->apn_data[j].apn);
                }
                p=p+2;
            }
        }
    }
}
	